package lance.honeydew.dao;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

import lance.honeydew.bean.UserMember;

import org.hibernate.FlushMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;

/**
 * Basic CRUD V1.0
 * Release date 2012.03.17
 * @author Ken
 *
 */

public class RootDAO extends HibernateDaoSupport {
	
	public RootDAO() {
		
    }
	
	public Session getSession() {
		
		Session session = this.getSessionFactory().openSession();
		
		return session;
	}
	
	public void create(Object obj){
		Session session = getSession();
		session.setFlushMode(FlushMode.ALWAYS);
		Transaction tx = session.beginTransaction();
		session.save(obj);
		tx.commit();
		session.close();
	}
	
	public void update(Object obj){
		Session session = getSession();
		session.setFlushMode(FlushMode.ALWAYS);
		Transaction tx = session.beginTransaction();
		session.update(obj);
		tx.commit();
		session.close();
	}
	
	public void delete(Object obj){
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		session.delete(obj);
		tx.commit();
		session.close();
	}
	
	public void delete(String field, Object obj) {
		Session session = getSession();
		session.setFlushMode(FlushMode.ALWAYS);
		Transaction tx= session.beginTransaction();		
		session.delete(field, obj);		
		tx.commit();
        session.close();
	}
	
	public Object findById(Class clazz, String prop, Long id) {
		
		DetachedCriteria criteria = DetachedCriteria.forClass(clazz);
		Object result = null;
		
		criteria.add(Restrictions.eq(prop, id));
		result = this.getHibernateTemplate().findByCriteria(criteria).get(0);
		
		return result;
		
	}
	
	/*public Object findById(String prop, Object obj) {
		
		try {
			
			Field field = obj.getClass().getField(prop);
			String methodName = new StringBuffer("get").append(field.get(obj).toString()).toString();
			Method method = obj.getClass().getMethod(methodName, null);
			
			// get id value
			Object key = method.invoke(obj, null);
			Serializable _id = (Serializable) key; 
			
			Session session = getSession();
			session.setFlushMode(FlushMode.ALWAYS);
			Transaction tx = session.beginTransaction();
			Object result = session.get(obj.getClass().getName(), _id); 
		    tx.commit();
			return result;
			
			
		} catch (RuntimeException re) {
			throw re;
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		
		return null;
	}*/
	
	/*
	public List findByExample(Object obj) {
		try {	
			Session session = sessionFactory.getCurrentSession();
			Transaction tx = session.beginTransaction();
			List results = session.createCriteria(obj.getClass().getName()).add(Example.create(obj)).list();
			tx.commit();
			
			return results;
		} catch (RuntimeException re) {
			throw re;
		} 
	}*/
}
